package com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestWorklist {
    public static void main(String[] args) {
//        String filePath = "D:\\data\\1233.cz"; // 替换为你的文件路径
        String filePath = "D:\\data\\GONGDANG1.cz"; // 替换为你的文件路径
        List<String> lines = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        Map<String, Integer> map1 = new HashMap<>();
        int blockSize = 50; // 每块的大小

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                if(line.indexOf("=") > 0) {
                    String tmp = line.substring(line.indexOf("=") + 1);
                    System.out.println(tmp);
                    lines.add(tmp);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将 List 转换为数组
        String[] array = lines.toArray(new String[0]);


        for (int i = 0; i < array.length; i += blockSize) {
            int end = Math.min(i + blockSize, array.length);
            String currentDrillinfo = array[i];

            String temp = "";
            if(i % 50 == 0) {
                temp = currentDrillinfo;
                boolean bflag = false;
                int maxSize = 50;
                if((array.length - i) > 50) {
                    for (int j = i; j < (array.length - j); j++) {
                        if(!currentDrillinfo.equals(array[j])) {
//                            System.out.println(currentDrillinfo);
//                            System.out.println(array[j]);
                            bflag = true;
                            break;
                        }
                    }
//                    if(!bflag) {
//                        System.out.println(i);
//                        System.out.println(bflag);
//                    }
                }
            }








            // 检查当前块的所有元素是否相同
            boolean allSame = true;
            for (int j = i; j < end; j++) {
                if (!array[j].equals(currentDrillinfo)) {
                    allSame = false;
                    break;
                }
                if((j+1) >= end) {
//                    System.out.println(i);
                }
            }

            if (allSame) {
//                map1.put(currentDrillinfo, map1.get(currentDrillinfo) == null ? 0 : ((Integer)map1.get(currentDrillinfo)).intValue() + blockSize);
//                map1.put(currentDrillinfo, map1.get(currentDrillinfo) == null ? 0 : ((Integer)map1.get(currentDrillinfo)).intValue() + 1);
                if (map1.containsKey(currentDrillinfo)) {
                    map1.put(currentDrillinfo, (Integer) map1.get(currentDrillinfo) + blockSize);
//                    System.out.println(i);
                } else {
                    map1.put(currentDrillinfo, blockSize);
//                    System.out.println(i);
                }
/*
                Integer fullCount = map1.get(currentDrillinfo);
                if(fullCount == null) {
                    fullCount = blockSize;
                } else {
                    fullCount = fullCount + blockSize;
                }
                map1.put(currentDrillinfo, fullCount);
*/

//                map1.put(currentDrillinfo, blockSize);
            }

            // 更新 map 的统计信息
            for (int j = i; j < end; j++) {
                String drillinfo = array[j];
                if (!map.containsKey(drillinfo)) {
                    map.put(drillinfo, 1);
                } else {
                    map.put(drillinfo, map.get(drillinfo) + 1);
                }
            }
        }



        // 输出结果
        System.out.println("统计每个元素的出现次数: " + map);
        System.out.println("连续50个相同元素的记录: " + map1);
    }
}
